package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.AboutUsDaoImpl;
import com.ysd.entity.AboutUs;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


public class AdminAboutUsServlet extends BaseServlet {
	
   /**
    * 查询出设计师的所有数据
    * @param localhost:8080/shangcheng1/adminAbout?reqName=selAbout
    * @param response
    * @throws ServletException
    * @throws IOException
    */
	public void selAbout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		AboutUsDaoImpl abDao = new AboutUsDaoImpl();
		
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		List<AboutUs> abLists = abDao.getAboutUsByPage(page, size);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		Gson gson = new Gson();
		String res = gson.toJson(abLists);
		out.print(res);
		
		out.flush();
		out.close();
		
	}
	 
	/**
	 * localhost:8080/shangcheng1/adminAbout?reqName=selAllAbout
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void selAllAbout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		AboutUsDaoImpl abDao = new AboutUsDaoImpl();
		
		
		List<AboutUs> abLists = abDao.getAboutUs();
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		Gson gson = new Gson();
		String res = gson.toJson(abLists.size());
		out.print(res);
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * 根据Id删除数据
	 * localhost:8080/shangcheng1/adminAbout?reqName=delAboutUs
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delAboutUs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int dId = Integer.parseInt(request.getParameter("peopleId"));
		AboutUsDaoImpl abDao = new AboutUsDaoImpl();
		int n = abDao.delAboutUs(dId);//调用实现类的删除方法
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();//创建out对象
		
		if (n > 0) {//删除成功
			out.print(Result.toClient(true, "删除成功", ""));
		} else {//删除失败
			out.print(Result.toClient(false, "删除失败", ""));
		}
		
		out.flush();
		out.close();
		
	}

	/**
	 * localhost:8080/shangcheng1/adminAbout?reqName=addAboutUs
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addAboutUs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out  = response.getWriter();
		AboutUsDaoImpl abDao = new AboutUsDaoImpl();

		String headPortrait = request.getParameter("headPortrait");
		String peopleName = request.getParameter("peopleName");
		String position = request.getParameter("position");
		
		if(peopleName!=null && !peopleName.equals("")) {
			List<AboutUs> ablist = abDao.getAboutByPeoName(peopleName);
			System.out.println(ablist);
			if (ablist.size()==0) {
				AboutUs ab = new AboutUs(0, headPortrait, peopleName, position);
				System.out.println(ab);
				int n = abDao.addAboutUs(ab);
				
				if(n > 0) {
					out.print(Result.toClient(true, "添加成功", ""));
				} else {
					out.print(Result.toClient(false, "添加失败", ""));
				}
			} else {
				out.print(Result.toClient(false, "设计师存在", ""));
			}
		}
		
		
		out.flush();
		out.close();
		
	}

	/**
	 * localhost:8080/shangcheng1/adminAbout?reqName=getAboutUsById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getAboutUsById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out  = response.getWriter();
		AboutUsDaoImpl abDao = new AboutUsDaoImpl();

		int abId = Integer.parseInt(request.getParameter("peopleId"));
		AboutUs ab = abDao.getAboutUsById(abId);
		
		Gson gson = new Gson();
		String res = gson.toJson(ab);
		out.print(res);
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * localhost:8080/shangcheng1/adminAbout?reqName=updateAboutUsById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateAboutUsById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out  = response.getWriter();
		AboutUsDaoImpl abDao = new AboutUsDaoImpl();

		int peopleId = Integer.parseInt(request.getParameter("peopleId"));
		String headPortrait = request.getParameter("headPortrait");
		String peopleName = request.getParameter("peopleName");
		String position = request.getParameter("position");
		AboutUs ab = new AboutUs(peopleId, headPortrait, peopleName, position);
		int n = abDao.updateAboutUs(ab);
		
		if (n > 0) {
			out.print(Result.toClient(true, "修改成功", ""));
		} else {
			out.print(Result.toClient(false, "修改失败", ""));
		}
		
		out.flush();
		out.close();
		
	}
	
	/**
	 * localhost:8080/shangcheng1/adminAbout?reqName=delAboutUsByIds
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delAboutUsByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out  = response.getWriter();
		AboutUsDaoImpl abDao = new AboutUsDaoImpl();

		String ids = request.getParameter("ids");
		int n = abDao.delAboutUsByIds(ids);
		
		if (n > 0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));
		}
		
		out.flush();
		out.close();
		
	}
	
}
